<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡查点位" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入点位名称/代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否巡查" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择是否巡查" clearable style="width:120px">
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskPointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="点位名称" align="center" prop="name" />
      <el-table-column label="点位编码" align="center" prop="code" />
      <el-table-column label="是否巡查" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="imgs" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改点位打卡对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taskPointRef" :model="form" :rules="rules" label-width="80px" disabled>
        <el-form-item label="点位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入点位名称" />
        </el-form-item>
        <el-form-item label="点位编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入点位编码" />
        </el-form-item>
        <el-form-item label="是否巡查" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片" prop="imgs">
          <image-upload v-model="form.imgs"/>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TaskPoint">
import { listTaskPoint, getTaskPoint, delTaskPoint, addTaskPoint, updateTaskPoint } from "@/api/patrol/taskPoint";

const { proxy } = getCurrentInstance();
const { sys_yes_no } = proxy.useDict('sys_yes_no');
const route = useRoute(); 

const taskPointList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    taskId: null,
    searchValue: null,
    name: null,
    code: null,
    status: null,
  },
  rules: {
    taskId: [
      { required: true, message: "任务不能为空", trigger: "blur" }
    ],
    pathId: [
      { required: true, message: "路线不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "点位名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询点位打卡列表 */
function getList() {
  loading.value = true;
  listTaskPoint(queryParams.value).then(response => {
    taskPointList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    taskId: null,
    pathId: null,
    name: null,
    code: null,
    lng: null,
    lat: null,
    alt: null,
    status: null,
    imgs: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("taskPointRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 修改按钮操作 */
function handleView(row) {
  reset();
  const _id = row.id || ids.value
  getTaskPoint(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看点位打卡详细";
  });
}


let taskId = route.params && route.params.taskId;

queryParams.value.taskId = taskId;

getList();
</script>
